package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.databinding.ItemCarHandlerDetail1Binding
import com.c.ctgproject.entity.guang.CARHandlerDetailListEntity
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.textColorResource
import java.lang.StringBuilder

class CarHandlerDetailAdapter1(private val context: Context, private val mList: ArrayList<CARHandlerDetailListEntity.CarDismantleOrderBean>) : RecyclerView.Adapter<CarHandlerDetailAdapter1.MyViewHolder>() {


    inner class MyViewHolder(view: ItemCarHandlerDetail1Binding) : RecyclerView.ViewHolder(view.root) {
        val mOrderNumberTV: TextView = view.mOrderNumberTV
        val mStatusTV: TextView = view.mStatusTV
        val mCreateNameTV: TextView = view.mCreateNameTV
        val mFinishTimeTV: TextView = view.mFinishTimeTV
        val mModelTV: TextView = view.mModelTV
        val mTimeTV: TextView = view.mTimeTV
        val mPlanTimeTV: TextView = view.mPlanTimeTV
        val mFinishTimeLL: LinearLayoutCompat = view.mFinishTimeLL
        val mParentLL: LinearLayoutCompat = view.mParentLL
        val mRecyclerView = view.mRecyclerView
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val binding = ItemCarHandlerDetail1Binding.inflate(LayoutInflater.from(context), parent, false)
        return MyViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val data = mList[position]
        data.dismantleNo.noDataHandler().also { holder.mOrderNumberTV.text = it }
        holder.mCreateNameTV.text = data.createPersonName.noDataHandler()
        holder.mTimeTV.text = data.createTime.noDataHandler()
        val dismantleMethod = when (data.dismantleMethod) {
            1 -> "毁型"
            2 -> "拆配件"
            else -> "暂无数据"
        }
        holder.mModelTV.text = dismantleMethod
        holder.mPlanTimeTV.text = data.plainFinishTime.noDataHandler()
        holder.mFinishTimeTV.text = data.realFinishTime.noDataHandler()
        holder.mFinishTimeLL.visibility = if (data.dismantleStatus == 2) View.VISIBLE else View.GONE
        data.infoList?.let { list ->
            holder.mRecyclerView.adapter = CarHandlerDetailAdapter2(context, list)
            holder.mRecyclerView.layoutManager = LinearLayoutManager(context)
        }
        //拆解单状态 0-待拆解 1-拆解中 2-拆解完成
        when (data.dismantleStatus) {
            0 -> {
                holder.mStatusTV.textColorResource = R.color.colorAccent
                holder.mStatusTV.text = "待拆解"
            }
            1 -> {
                holder.mStatusTV.text = "拆解中"
                holder.mStatusTV.textColorResource = R.color.red
            }
            2 -> {
                holder.mStatusTV.textColorResource = R.color.gray_text
                holder.mStatusTV.text = "拆解完成"
            }
            else -> {
                holder.mStatusTV.text = ""
            }
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }
}